package com.heima.controller;
import com.heima.utils.AliOssUtil;
import io.swagger.annotations.Api;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.UUID;

@RestController
@Api(tags = "文件上传")
@Slf4j
public class GoodsImgController {
    // Bean 有三种注入方式，1属性注入 2构造器注入 3setter注入
    @Autowired
    private  AliOssUtil aliOssUtil;

    /**
     * 图片上传
     * @return
     */
    @PostMapping("/api/vm-service/sku/fileUpload")
    public String upload( MultipartFile fileName) throws IOException {
        log.info("文件上传 {}",fileName);
        String filename = fileName.getOriginalFilename();

        String postfix = filename.substring(filename.lastIndexOf("."));
        String newFilename = UUID.randomUUID().toString()+ postfix;
        String url = aliOssUtil.upload(fileName.getBytes(),newFilename);
        return url;



    }

}
